/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converter;

import entity.Comune;
import entity.Negozio;
import java.util.StringTokenizer;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Carmela
 */
@FacesConverter(forClass=Negozio.class)
public class NegozioConverter implements Converter{

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        StringTokenizer st = new StringTokenizer(value, "#&");
        Negozio res=new Negozio();
        Comune c=new Comune();
        int i = 0;
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            switch (i) {
                case 0:
                    res.setId(Long.parseLong(token));
                    break;
                case 1:
                    res.setNome(token);
                    break;
                case 2:
                    c.setId(Long.parseLong(token));
                    break;
                case 3:
                    res.setComune(c);
                    return res;
            }
            i++;
        }
        res.setComune(c);
        return res;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if(value!=null){
            return value.toString();
        }
        throw new IllegalStateException();
    }
    
}
